﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ShopOnline
{
    /// <summary>
    /// TODO:Put a description here.
    /// </summary>
    [Serializable]
    public class ProductRec
    {
        #region Private Members
        private Int64 m_id = 0;
        private Int64 m_categoryId = 0;
        private String m_name = String.Empty;
        private String m_sid = String.Empty;
        private Decimal m_price = 0;
        private String m_imgUrl = String.Empty;
        private String m_description = String.Empty;
        private String m_content = String.Empty;
        private DateTime m_createDate = DateTime.MinValue;
        private int m_status = -1;
        #endregion

        #region Public Properties
        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 categoryId
        {
            get { return m_categoryId; }
            set { m_categoryId = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String sid
        {
            get { return m_sid; }
            set { m_sid = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Decimal price
        {
            get { return m_price; }
            set { m_price = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String imgUrl
        {
            get { return m_imgUrl; }
            set { m_imgUrl = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String content
        {
            get { return m_content; }
            set { m_content = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public DateTime createDate
        {
            get { return m_createDate; }
            set { m_createDate = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public int status
        {
            get { return m_status; }
            set { m_status = value; }
        }

        #endregion

        #region  Properties Const

        private const string ConnectName = "ShopOnline";
        private const string Folderlogs = "Product";

        #endregion

        #region Store Procedure
        private String usp_Add_Product = "usp_Add_Product";
        private String usp_Update_Product = "usp_Update_Product";
        private String usp_Delete_Product = "usp_Delete_Product";

        private String usp_GetDetail_Product = "usp_GetDetail_Product";
        private String usp_GetListByCategory_Product = "usp_GetListByCategory_Product";
        private String usp_GetListByDomain_Product = "usp_GetListByDomain_Product";
        private String usp_SearchPriceByDomain_Product = "usp_SearchPriceByDomain_Product";
        #endregion

        #region Init
        public ProductRec() { }
        public ProductRec(Int64 id, Int64 categoryId, String name, String sid, Decimal price, String imgUrl, String description, String content, DateTime createDate, int status)
        {
            m_id = id;
            m_categoryId = categoryId;
            m_name = name;
            m_sid = sid;
            m_price = price;
            m_imgUrl = imgUrl;
            m_description = description;
            m_content = content;
            m_createDate = createDate;
            m_status = status;
        }
        #endregion

        #region Call Store Procedure
        public ObjectResult AddProduct(Int64 DomainID,Int64 ProductID, Int64 CategoryID, string ProductName,string ProductSeoID, Decimal ProductPrice, string ProductImgUrl, string ProductDes, string ProductContent ) {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "AddProduct - DomainID:" + DomainID + ", ProductID:" + ProductID + ", CategoryID:" + CategoryID + ", ProductName:" + ProductName;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Add_Product, DomainID, ProductID, CategoryID, ProductName, ProductSeoID, ProductPrice, ProductImgUrl, ProductDes, ProductContent);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public ObjectResult UpdateProduct(Int64 DomainID, Int64 ProductID, string ProductName, string ProductSeoID, Decimal ProductPrice, string ProductImgUrl, string ProductDes, string ProductContent) {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "UpdateProduct - DomainID:" + DomainID + ", ProductID:" + ProductID + ", ProductName:" + ProductName;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Update_Product, DomainID, ProductID, ProductName, ProductSeoID, ProductPrice, ProductImgUrl, ProductDes, ProductContent);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public ObjectResult DeleteProduct(Int64 DomainID , Int64 ProductID) {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "DeleteProduct - DomainID:" + DomainID + ", ProductID:" + ProductID;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Delete_Product, DomainID, ProductID);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public ProductRec GetDetail(Int64 ProductID)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetDetail - ProductID:" + ProductID;
            try
            {
                ProductRec objProduct = new ProductRec();

                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetDetail_Product, ProductID);

                if (dsResult.Tables[0].Rows.Count > 0)
                {
                    objProduct.id = Convert.ToInt64(dsResult.Tables[0].Rows[0]["id"].ToString());
                    if (!dsResult.Tables[0].Rows[0].IsNull("categoryId"))
                    {
                        objProduct.categoryId = (Int64)dsResult.Tables[0].Rows[0]["categoryId"];
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("name"))
                    {
                        objProduct.name = dsResult.Tables[0].Rows[0]["name"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("sid"))
                    {
                        objProduct.sid = dsResult.Tables[0].Rows[0]["sid"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("price"))
                    {
                        objProduct.price = (Decimal)dsResult.Tables[0].Rows[0]["price"];
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("imgUrl"))
                    {
                        objProduct.imgUrl = dsResult.Tables[0].Rows[0]["imgUrl"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("description"))
                    {
                        objProduct.description = dsResult.Tables[0].Rows[0]["description"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("content"))
                    {
                        objProduct.content = dsResult.Tables[0].Rows[0]["content"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("createDate"))
                    {
                        objProduct.createDate = (DateTime)dsResult.Tables[0].Rows[0]["createDate"];
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("status"))
                    {
                        objProduct.status = (int)dsResult.Tables[0].Rows[0]["status"];
                    }

                }
                else
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                    objProduct = null;
                }

                return objProduct;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return null;
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public List<ProductRec> GetListByDomainID(Int64 DomainID, int PageSize, int PageNumber, ref int Total)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetListByDomainID - PageSize:" + PageSize + ",PageNumber: " + PageNumber;
            try
            {
                List<ProductRec> records = new List<ProductRec>();
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetListByDomain_Product,DomainID, PageSize, PageNumber);

                #region Default variables
                Int64 id = 0;
                Int64 categoryid = 0;
                String name = String.Empty;
                String sid = String.Empty;
                Decimal price = 0;
                String imgurl = String.Empty;
                String description = String.Empty;
                String content = String.Empty;
                DateTime createdate = DateTime.MinValue;
                int status = -1;
                #endregion

                if (dsResult.Tables.Count == 2)
                {
                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        if (row.IsNull("id") == false)
                            id = (Int64)row["id"];
                        if (row.IsNull("categoryId") == false)
                            categoryid = (Int64)row["categoryId"];
                        if (row.IsNull("name") == false)
                            name = row["name"].ToString();
                        if (row.IsNull("sid") == false)
                            sid = row["sid"].ToString();
                        if (row.IsNull("price") == false)
                            price = (Decimal)row["price"];
                        if (row.IsNull("imgUrl") == false)
                            imgurl = row["imgUrl"].ToString();
                        if (row.IsNull("description") == false)
                            description = row["description"].ToString();
                        if (row.IsNull("content") == false)
                            content = row["content"].ToString();
                        if (row.IsNull("createDate") == false)
                            createdate = (DateTime)row["createDate"];
                        if (row.IsNull("status") == false)
                            status = (int)row["status"];
                        records.Add(new ProductRec(id, categoryid, name, sid, price, imgurl, description, content, createdate, status));
                    }
                    Total = (int)dsResult.Tables[1].Rows[0][0];
                }
                if (records.Count <= 0)
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                    Total = 0;
                }
                return records;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new List<ProductRec>();
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public List<ProductRec> GetListByCategoryID(Int64 DomainID, Int64 CategoryID, int PageSize, int PageNumber, ref int Total)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetListByDomainID - PageSize:" + PageSize + ",PageNumber: " + PageNumber;
            try
            {
                List<ProductRec> records = new List<ProductRec>();
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetListByCategory_Product, DomainID, CategoryID, PageSize, PageNumber);

                #region Default variables
                Int64 id = 0;
                Int64 categoryid = 0;
                String name = String.Empty;
                String sid = String.Empty;
                Decimal price = 0;
                String imgurl = String.Empty;
                String description = String.Empty;
                String content = String.Empty;
                DateTime createdate = DateTime.MinValue;
                int status = -1;
                #endregion

                if (dsResult.Tables.Count == 2)
                {
                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        if (row.IsNull("id") == false)
                            id = (Int64)row["id"];
                        if (row.IsNull("categoryId") == false)
                            categoryid = (Int64)row["categoryId"];
                        if (row.IsNull("name") == false)
                            name = row["name"].ToString();
                        if (row.IsNull("sid") == false)
                            sid = row["sid"].ToString();
                        if (row.IsNull("price") == false)
                            price = (Decimal)row["price"];
                        if (row.IsNull("imgUrl") == false)
                            imgurl = row["imgUrl"].ToString();
                        if (row.IsNull("description") == false)
                            description = row["description"].ToString();
                        if (row.IsNull("content") == false)
                            content = row["content"].ToString();
                        if (row.IsNull("createDate") == false)
                            createdate = (DateTime)row["createDate"];
                        if (row.IsNull("status") == false)
                            status = (int)row["status"];
                        records.Add(new ProductRec(id, categoryid, name, sid, price, imgurl, description, content, createdate, status));
                    }
                    Total = (int)dsResult.Tables[1].Rows[0][0];
                }
                if (records.Count <= 0)
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                    Total = 0;
                }
                return records;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new List<ProductRec>();
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public List<ProductRec> SearchPriceByDomain(Int64 DomainID, String KeySearch,Decimal MinPrice, Decimal MaxPrice, int PageSize, int PageNumber, ref int Total)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "SearchPriceByDomain - DomainID:" + DomainID + ",KeySearch:" + KeySearch + ",MinPrice:" + MinPrice + ",MaxPrice:" + MaxPrice + ",PageSize:" + PageSize + ",PageNumber: " + PageNumber;
            try
            {
                List<ProductRec> records = new List<ProductRec>();
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                String t_KeySearch = null;
                if (!String.IsNullOrEmpty(KeySearch))
                {
                    t_KeySearch = KeySearch;
                }
                Decimal? t_MinPrice = null;
                if(MinPrice > 0){
                    t_MinPrice = MinPrice;
                }

                Decimal? t_MaxPrice = null;
                if (MaxPrice > 0) {
                    t_MaxPrice = MaxPrice;
                }

                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_SearchPriceByDomain_Product, DomainID, t_KeySearch, t_MinPrice, t_MaxPrice, PageSize, PageNumber);

                #region Default variables
                Int64 id = 0;
                Int64 categoryid = 0;
                String name = String.Empty;
                String sid = String.Empty;
                Decimal price = 0;
                String imgurl = String.Empty;
                String description = String.Empty;
                String content = String.Empty;
                DateTime createdate = DateTime.MinValue;
                int status = -1;
                #endregion

                if (dsResult.Tables.Count == 2)
                {
                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        if (row.IsNull("id") == false)
                            id = (Int64)row["id"];
                        if (row.IsNull("categoryId") == false)
                            categoryid = (Int64)row["categoryId"];
                        if (row.IsNull("name") == false)
                            name = row["name"].ToString();
                        if (row.IsNull("sid") == false)
                            sid = row["sid"].ToString();
                        if (row.IsNull("price") == false)
                            price = (Decimal)row["price"];
                        if (row.IsNull("imgUrl") == false)
                            imgurl = row["imgUrl"].ToString();
                        if (row.IsNull("description") == false)
                            description = row["description"].ToString();
                        if (row.IsNull("content") == false)
                            content = row["content"].ToString();
                        if (row.IsNull("createDate") == false)
                            createdate = (DateTime)row["createDate"];
                        if (row.IsNull("status") == false)
                            status = (int)row["status"];
                        records.Add(new ProductRec(id, categoryid, name, sid, price, imgurl, description, content, createdate, status));
                    }
                    Total = (int)dsResult.Tables[1].Rows[0][0];
                }
                if (records.Count <= 0)
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                    Total = 0;
                }
                return records;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new List<ProductRec>();
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }
        #endregion
    }

}
